﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ServiceModel;

namespace Harmonic.AdministratorTool
{
    /// <summary>
    /// Interaction logic for ChangeUserStatus.xaml
    /// </summary>
    public partial class ChangeUserStatus : Window
    {
        public ChangeUserStatus()
        {
            InitializeComponent();
        }

        private StateChangeType _stateChangeType;
        private Guid _userId;
        private int _userVersion;
        private bool _userChanged = false;

        public enum StateChangeType
        {
            Upgrade,
            Downgrade,
            Delete
        }

        public bool ShowForm(Guid userId, int userVersion, StateChangeType stateChangeType)
        {
            _stateChangeType = stateChangeType;
            _userId = userId;
            _userVersion = userVersion;

            if (_stateChangeType == StateChangeType.Upgrade)
                lblLabel.Content = "Why is this Customer being upgraded?";
            else
                lblLabel.Content = "Why is this Customer being downgraded?";

            this.ShowDialog();
            return _userChanged;
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_stateChangeType == StateChangeType.Upgrade)
                    CqrsService.Client.BeginUpgradeUser(_userId, txtReason.Text, _userVersion, null, null);
                else if (_stateChangeType == StateChangeType.Downgrade)
                    CqrsService.Client.DowngradeUser(_userId, txtReason.Text, _userVersion);
                    //CqrsService.Client.BeginUpgradeUser(_userId, txtReason.Text, _userVersion, callback, null);
                else if (_stateChangeType == StateChangeType.Delete)
                    CqrsService.Client.BeginDeleteUser(_userId, txtReason.Text, _userVersion, null, null);

                _userChanged = true;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void callback(IAsyncResult ar)
        {
            try
            {
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
